package com.sticktogether.usermanagement;


import android.os.Handler;
import android.os.Looper;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.sticktogether.server.ServerCommunication;

/**
 * Members are groupmembers, except the app user
 * @author David Schattel
 *
 */
public class Member{

	private final int	 		mId;
	private final String		username;
	private LatLng				position;
	private String				last_gps_update;
	public Marker				mMarker;
	private BitmapDescriptor	bitmapDescriptor;
	
	
	/**
	 * Member defined with URL of its profile picture
	 * @param id
	 * @param name
	 * @param last_gps_update
	 * @param latitude
	 * @param longitude
	 * @param urlString
	 */
	public Member(int id, String name, String last_gps_update, float latitude, float longitude, BitmapDescriptor bitmapDescriptor){
		mId = id;
		username = name;
		this.last_gps_update = last_gps_update;
		position = new LatLng(latitude,longitude);
		this.bitmapDescriptor = bitmapDescriptor;
	}
	
	/**
	 * Update the location of the marker
	 * @param loc new Location as float array
	 */
	public BitmapDescriptor getBitmapDescriptor(){
		return bitmapDescriptor;
	}
	
	public int getId() {
		return mId;
	}

	public String getUsername() {
		return username;
	}
	
	/**
	 * Set age of location to add it to the infobox
	 * 
	 * @param last_gps_update
	 */
	public void setLastUpdateTime(String last_gps_update){
		this.last_gps_update = last_gps_update;
		if(mMarker != null) {
			Handler handler = new Handler(Looper.getMainLooper());
			handler.post(new Runnable(){
				public void run() {
					mMarker.setTitle(getUsername());
					mMarker.setSnippet(getLastUpdateTime());
					// If the user is selected at the moment, update the info window with the new time and center the camera on the user (with the current zoom level).
					if(mMarker.isInfoWindowShown()) {
						mMarker.showInfoWindow();
						ServerCommunication.map.mMapView.animateCamera(CameraUpdateFactory.newLatLngZoom(getLocation(), ServerCommunication.map.mMapView.getCameraPosition().zoom));
					}
				}
			});
		}
	}
	
	/**
	 * Get Time of last location update
	 * @return
	 */
	public String getLastUpdateTime() {
		return last_gps_update;
	}
	
	/**
	 * set current location
	 * @param lat
	 * @param lng
	 */
	public void setLocation(final double lat, final double lng){
		position = new LatLng(lat, lng);
		if(mMarker != null) {
			Handler handler = new Handler(Looper.getMainLooper());
			handler.post(new Runnable(){
				public void run() {
					mMarker.setPosition(position);
				}
			});
		}
	}
	
	
	public LatLng getLocation() {
		return position;
	}


	public double getLatitude() {
		return position.latitude;
	}


	public double getLongitude() {
		return position.longitude;
	}

}
